{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# BrainFlow to MNE Python Notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import brainflow\n",
    "from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds\n",
    "\n",
    "from mne.viz.topomap import _prepare_topo_plot, plot_topomap\n",
    "import mne\n",
    "from mne.channels import read_layout\n",
    "\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# use synthetic board for demo\n",
    "params = BrainFlowInputParams ()\n",
    "board = BoardShim (BoardIds.SYNTHETIC_BOARD.value, params)\n",
    "board.prepare_session ()\n",
    "board.start_stream ()\n",
    "time.sleep (10)\n",
    "data = board.get_board_data ()\n",
    "board.stop_stream ()\n",
    "board.release_session ()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eeg_channels = BoardShim.get_eeg_channels (BoardIds.SYNTHETIC_BOARD.value)\n",
    "eeg_data = data[eeg_channels, :]\n",
    "eeg_data = eeg_data / 1000000 # BrainFlow returns uV, convert to V for MNE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Creating MNE objects from brainflow data arrays\n",
    "ch_types = ['eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg', 'eeg']\n",
    "ch_names = ['T7', 'CP5', 'FC5', 'C3', 'C4', 'FC6', 'CP6', 'T8']\n",
    "sfreq = BoardShim.get_sampling_rate (BoardIds.SYNTHETIC_BOARD.value)\n",
    "info = mne.create_info (ch_names = ch_names, sfreq = sfreq, ch_types = ch_types)\n",
    "raw = mne.io.RawArray (eeg_data, info)\n",
    "# its time to plot something!\n",
    "raw.plot_psd (average = True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
